package com.example.smartcity_poverty.ui.AllService.LogisticsQuery.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.context
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.LogisticsQueryBean.LqNoQueryModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.LogisticsQuery.adapter.LqShowThisAdapter
import kotlinx.android.synthetic.main.activity_lq_dan_hao.*
import kotlinx.coroutines.Dispatchers

/**
 * 单号查询
 */
class LqDanHaoActivity : AppCompatActivity() {

    private val content by lazy { intent.getStringExtra("content") }
    private var no: String? = null

    // 物流公司 ID
    private var lqId: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_lq_dan_hao)
        title = "单号查询"

        no = content

        runThread()

        lqSeekQuery.setOnEditorActionListener { textView, i, keyEvent ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                no = lqSeekQuery.text.toString()
                coroutine(Dispatchers.Main) {
                    // 填充数据
                    setSeek()
                    // 更新物流信息
                    setList()
                }
            }
            false
        }


        // 点击投诉
        lqShowThisOk.setOnClickListener {
            startActivity(Intent(this, LqMeTouSuActivityActivity::class.java).apply {
                putExtra("id", no.toString())
            })
        }
    }


    private fun runThread() = coroutine(Dispatchers.Main) {
        // 填充数据
        setSeek()

        // 更新物流信息
        setList()
    }

    // 更新物流数据
    private val arrayList = ArrayList<LqNoQueryModel.DataBean.StepListBean>()
    private suspend fun setList() {
        api.getLqNoQuery(getToken(), no!!).apply {
            if (this.code == 200) {

                // 显示投诉按钮
                lqShowThisOk.visibility = View.VISIBLE

                arrayList.addAll(this.data?.stepList!!)
                runOnUiThread {
                    lqShowListQuery.apply {
                        this.isNestedScrollingEnabled = false
                        this.layoutManager = LinearLayoutManager(context)
                        this.adapter = LqShowThisAdapter(arrayList, R.layout.re_lq_no_list)
                        this.adapter?.notifyItemChanged(arrayList.size)
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }

    private suspend fun setSeek() {
        api.getLqNoQuery(getToken(), no!!).apply {
            if (this.code == 200) {
                runOnUiThread {
                    this.data?.let {
                        lqSeekQuery.setText(content)
                        glide(url + it.company?.imgUrl, lqShowImageQuery)
                        lqShowNameQuery.text = it.company?.name
                        lqShowPhoneQuery.text = it.company?.phone

                        // 物流公司 ID
                        lqId = it.id.toString()
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}